import { //核心
    React, Enhance, Controller, Component, $, EventEmitter,
    //
    tools, classnames,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Http,
    BoxWidget, Input, Button, Prompt, Link, Loaders, Editor, Resource, Directive, Lock, Fix
} from 'admin';
import { exp } from 'locutus/php/math';

/**
 * 供应商
 */
class Supplier extends Component {

    /**
     * 仓储对象缓存
     */
    static cache = null
    state = {
        edit: {}
    }

    _lang(name){
        return `ysw.supply.${name}`;
    }

    /**
       * componentDidMount()在挂载结束之后马上被调用。需要DOM节点的初始化操作应该放在这里。
       */
    componentDidMount() {
        this.handlerLoadCache();
    }

    render() {
        let { edit } = this.state;
        return (
            <div>
                <Http ref="http"></Http>
                <Directive name="Modal">
                    <Dialog ref="addDialog" title={this._lang("add")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/supply/do_add/`}
                                onSuccess={this.handlerAddSuccess}
                                onError={this.handlerAddError}
                                rules={{
                                    "code": "required",
                                    "name": "required"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="code" type="text" message={this._lang("code")} />
                            <Input name="name" type="text" message={this._lang("name")} />
                            <Input name="address" type="text" message={this._lang("address")} />
                            <Input name="contact" type="text" message={this._lang("contact")} />
                            <Input name="position" type="text" message={this._lang("position")} />
                            <Input name="phone" type="number" message={this._lang("phone")} />
                            <Input name="fax" type="text" message={this._lang("fax")} />
                            <Input name="zip_code" type="text" message={this._lang("zip_code")} />
                            <Input name="ein" type="text" message={this._lang("ein")} />
                            <Input name="bank" type="text" message={this._lang("bank")} />
                            <Input name="bankcard" type="text" message={this._lang("bankcard")} />
                            <Input name="email" type="text" message={this._lang("email")} />
                            <Input name="www" type="text" message={this._lang("www")} />
                            <Input name="content" type="text" message={this._lang("content")} />
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
                <Directive name="Modal">
                    <Dialog ref="editDialog" title={this._lang("edit")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/supply/do_edit/`}
                                onSuccess={this.handlerEditSuccess}
                                onError={this.handlerEditError}
                                rules={{
                                    "code": "required",
                                    "name": "required"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="id" type="hidden" model={this.state.edit} />
                            <Input name="code" type="text" message={this._lang("code")} model={this.state.edit} />
                            <Input name="name" type="text" message={this._lang("name")} model={this.state.edit} />
                            <Input name="address" type="text" message={this._lang("address")} model={this.state.edit} />
                            <Input name="contact" type="text" message={this._lang("contact")} model={this.state.edit} />
                            <Input name="position" type="text" message={this._lang("position")} model={this.state.edit} />
                            <Input name="phone" type="number" message={this._lang("phone")} model={this.state.edit} />
                            <Input name="fax" type="text" message={this._lang("fax")} model={this.state.edit} />
                            <Input name="zip_code" type="text" message={this._lang("zip_code")} model={this.state.edit} />
                            <Input name="ein" type="text" message={this._lang("ein")} model={this.state.edit} />
                            <Input name="bank" type="text" message={this._lang("bank")} model={this.state.edit} />
                            <Input name="bankcard" type="text" message={this._lang("bankcard")} model={this.state.edit} />
                            <Input name="email" type="text" message={this._lang("email")} model={this.state.edit} />
                            <Input name="www" type="text" message={this._lang("www")} model={this.state.edit} />
                            <Input name="content" type="text" message={this._lang("content")} model={this.state.edit} />
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
            </div>
        );
    }

    /**
     * 加载缓存
     */
    handlerLoadCache = () => {
        if (!Supplier.cache) {
            this.refs.http.send(`${window.SERVER_URL}/ysw/supply/get_supply/`)
                .success((data) => {
                    Supplier.cache = data;
                    this.props.onCache && this.props.onCache()
                })
                .error();
        }
    }

    handlerAddSuccess = (data) => {
        //
        this.props.onAddSuccess && this.props.onAddSuccess(data);
        //
        if (data) {
            Supplier.cache = null;
            this.handlerLoadCache()
            this.hideAddDialog();
        }
    }

    handlerAddError = () => {

    }

    /**
     * 显示添加
     */
    showAddDialog() {
        this.refs.addDialog.show()
    }

    /**
     * 隐藏添加
     */
    hideAddDialog() {
        this.refs.addDialog.hide()
    }

    /**
     * 
     * @param {*} edit 
     */
    showEditDialog(edit) {
        this.setState({ edit }, () => {
            this.refs.editDialog.show();
        });
    }

    hideEditDialog() {
        this.refs.editDialog.hide();
    }

    /**
     * 
     */
    handlerEditSuccess = (data) => {
        //
        this.props.onEditSuccess && this.props.onEditSuccess(data);
        //
        if (data) {
            Supplier.cache = null;
            this.handlerLoadCache();
            this.hideEditDialog();

        }
    }

    handlerEditError = () => {
        this.props.onEditError && this.props.onEditError();
    }
}
Enhance(Supplier)
export default Supplier